<template>
  <div class="app-container" id="userContainer">
    <div style="margin-bottom: 30px">
      <el-row :gutter="20">
        <el-col :span="5">
          <el-input
            placeholder="昵称..."
            v-model="queryForm.query"
            clearable
            @keyup.enter="initUserList(1)"
          ></el-input>
        </el-col>
        <el-button
          type="primary"
          :icon="Search"
          @click="initUserList(1)"
          v-hasPerm="['vote:userInfo:query']"
          >搜索</el-button
        >
         <el-button @click="resetForm" :icon="Refresh">重置</el-button>
      </el-row>
    </div>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      :row-style="{ height: '40px' }"
      :cell-style="{ padding: '0px' }"
      row-class-name="table-row-cls"
    >
      <el-table-column label="头像" prop="avatar" width="80" align="center">
        <template v-slot="scope">
          <el-image
                    :src="pre_url + '/image/userAvatar/' + scope.row.avatarUrl"
                    :zoom-rate="1.2"
                    :preview-src-list="[pre_url + '/image/userAvatar/' + scope.row.avatarUrl]" 
                    hide-on-click-modal
                  />
        </template>
      </el-table-column>
      <el-table-column prop="nickName" label="昵称" align="center" />
      <el-table-column
        prop="email"
        label="邮箱"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column prop="phone" label="手机号" align="center" />
      <el-table-column prop="status" label="状态" align="center"   v-hasPerm="['vote:userInfo:edit']">
        <template v-slot="{ row }">
          <el-switch
            v-model="row.status"
            @change="statusChangeHandle(row)"
            active-value="0"
            inactive-value="1"
            size="small"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="registerDate"
        label="创建时间"
        align="center"
        width="180"
      />
      <el-table-column
        prop="lastLoginDate"
        label="最后登录时间"
        align="center"
        width="180"
      />
      <el-table-column prop="action" label="操作" width="150" align="center">
        <template v-slot="scope">
          <el-popconfirm
            title="您确定要重置密码吗"
            @confirm="resetPwdHandler(scope.row.id)"
            v-hasPerm="['vote:userInfo:edit']"
          >
            <template #reference>
              <el-button type="warning" :icon="RefreshRight" size="small" text
                >重置密码</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="queryForm.pageNum"
        v-model:page-size="queryForm.pageSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        :small="false"
        :disabled="false"
        :background="false"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getWUserList, updateWUserStatus, resetPwd } from "@/api/voteUser";
import {
  DArrowRight,
  RefreshRight,
  Search,
  Tools,
  Refresh
} from "@element-plus/icons-vue";

const tableData = ref([]);
const total = ref(0);
const pre_url = ref(import.meta.env.VITE_APP_WEB_API);
const queryForm = ref({
  query: "",
  pageNum: 1,
  pageSize: 5,
});
const userId = ref(-1);
  tableData.value = [];

const resetForm = () => {
  queryForm.value = {
    query: "",
    pageNum: 1,
    pageSize: 5,
  };
  initUserList(1);
};
const initUserList = async (pageNum?: number) => {
  if (pageNum) queryForm.value.pageNum = pageNum;
  let result = await getWUserList(queryForm.value);
  let res = result.data;
  if (res.code == 200) {
    tableData.value = res.data.list;
    total.value = res.data.total;
  }
};

const handleSizeChange = (val) => {
  queryForm.value.pageSize = val;
  console.log("queryForm", queryForm);
  initUserList();
};
const handleCurrentChange = (val) => {
  queryForm.value.pageNum = val;
  initUserList();
};

/**
 * 更新用户状态
 */

const statusChangeHandle = async (row) => {
  const res = await updateWUserStatus(row);
  const data = res.data;
  if (data.code == 200) {
      if (row.status == 0) {
      ElMessage({
        type: "success",
        message: "启用用户成功",
      });
    } else {
      ElMessage({
        type: "success",
        message: "禁用用户成功",
      });
    }
  } else {
    ElMessage({
      type: "error",
      message: data.msg,
    });
  }
};
const resetPwdHandler = async (id) => {
  const res = await resetPwd(id);
  const data = res.data;
  if (data.code == 200) {
    ElMessage({
      type: "success",
      message: data.msg,
    });
  } else {
    ElMessage({
      type: "error",
      message: data.msg,
    });
  }
};
onMounted(() => {
  initUserList();
});
</script>

<style lang="less" scoped>
.app-container {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}

.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

.el-table__cell {
  word-break: break-word;
  height: 40px;
  font-size: 13px;
}

.el-tag--small {
  margin-left: 5px;
}
</style>
<style lang="less">
.table-row-cls {
  .el-button + .el-button {
    margin-left: 0;
  }
}
</style>